﻿using System;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;

using DotNetUI.Controls;
using DotNetUI.Models;

namespace DotNetUI.Extensions
{
    /// <summary>
    /// 本地文件扩展
    /// </summary>
    public class DotnetImageExtension : MarkupExtension
    {
        /// <summary>
        /// 本地文件扩展
        /// </summary>
        public DotnetImageExtension()
        {

        }
        /// <summary>
        /// 本地文件扩展
        /// </summary>
        /// <param name="path"></param>
        public DotnetImageExtension(string path)
        {
            Path = path;
        }
        /// <summary>
        /// 缩放方式
        /// </summary>
        public Stretch Stretch { get; set; }
        /// <summary>
        /// 文件路径
        /// </summary>
        public string Path { get; set; }
        /// <summary>
        /// 水平对齐
        /// </summary>
        public AlignmentX AlignmentX { get; set; }
        /// <summary>
        /// 垂直对其
        /// </summary>
        public AlignmentY AlignmentY { get; set; }
        /// <summary>
        /// 透明度
        /// </summary>
        public double Opacity { get; set; }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            ImageBrush imageBrush = new ImageBrush();
            imageBrush.ImageSource = new BitmapImage(new Uri($"pack://SiteOfOrigin:,,,/{Path}", UriKind.RelativeOrAbsolute)) { };
            imageBrush.Stretch = Stretch;
            imageBrush.AlignmentX = AlignmentX;
            imageBrush.Opacity = Opacity;
            imageBrush.AlignmentY = AlignmentY;
            return imageBrush;
        }
    }
}
